package models

import (
	"github.com/astaxie/beego/logs"
	"github.com/astaxie/beego/orm"
)

func GetOriginUpstream(cveNum string, ou *OriginUpstream) (err error) {
	o := orm.NewOrm()
	err = o.Raw("select * FROM cve_origin_upstream "+
		"WHERE cve_num=? order by cve_id desc limit 1", cveNum).QueryRow(ou)
	if err == nil {
		logs.Info("GetOriginUpstream, cve_origin_upstream, "+
			"cveNum: ", cveNum, ", search successful")
		return nil
	} else {
		logs.Error("GetOriginUpstream, cve_origin_upstream, "+
			"cveNum: ", cveNum, ", Query failed")
	}
	return err
}

func GetOriginDesc(cveId int64, oud *OriginUpstreamDesc) (err error) {
	o := orm.NewOrm()
	err = o.Raw("select * FROM cve_origin_upstream_desc "+
		"WHERE cve_id=?", cveId).QueryRow(oud)
	if err == nil {
		logs.Info("GetOriginDesc, cve_origin_upstream_desc, "+
			"cve_id: ", cveId, ", search successful")
		return nil
	} else {
		logs.Error("GetOriginDesc, cve_origin_upstream_desc, "+
			"cve_id: ", cveId, ", Query failed")
	}
	return err
}

func GetOriginConfig(cveId int64, ouc *OriginUpstreamConfig) (err error) {
	o := orm.NewOrm()
	err = o.Raw("select * FROM cve_origin_upstream_config "+
		"WHERE cve_id=?", cveId).QueryRow(ouc)
	if err == nil {
		logs.Info("GetOriginConfig, cve_origin_upstream_config, "+
			"cve_id: ", cveId, ", search successful")
		return nil
	} else {
		logs.Error("GetOriginConfig, cve_origin_upstream_config, "+
			"cve_id: ", cveId, ", Query failed")
	}
	return err
}

func GetOriginConfigNode(configId int64, oucn *[]OriginUpstreamConfigNode) (num int64, err error) {
	o := orm.NewOrm()
	num, err = o.Raw("select * FROM cve_origin_upstream_config_node "+
		"WHERE conf_id=?", configId).QueryRows(oucn)
	if num > 0 && err == nil {
		logs.Info("GetOriginConfigNode, cve_origin_upstream_config_node, "+
			"configId: ", configId, ", search successful")
		return num, nil
	} else {
		logs.Error("GetOriginConfigNode, cve_origin_upstream_config_node,"+
			" configId: ", configId, ", Query failed")
	}
	return 0, err
}

func GetOriginConfigNodeCpe(nodeId int64, oucnc *[]OriginUpstreamConfigNodeCpe) (num int64, err error) {
	o := orm.NewOrm()
	num, err = o.Raw("select * FROM cve_origin_upstream_config_node_cpe "+
		"WHERE node_id=?", nodeId).QueryRows(oucnc)
	if num > 0 && err == nil {
		logs.Info("GetOriginConfigNodeCpe, cve_origin_upstream_config_node_cpe,"+
			" configId: ", nodeId, ", search successful")
		return num, nil
	} else {
		logs.Error("GetOriginConfigNodeCpe, cve_origin_upstream_config_node_cpe, "+
			"configId: ", nodeId, ", Query failed")
	}
	return 0, err
}

func GetOriginPoc(cveId int64, oup *OriginUpstreamPoc) (err error) {
	o := orm.NewOrm()
	err = o.Raw("select * FROM cve_origin_upstream_poc "+
		"WHERE cve_id=?", cveId).QueryRow(oup)
	if err == nil {
		logs.Info("GetOriginPoc, cve_origin_upstream_poc, "+
			"cve_id: ", cveId, ", search successful")
		return nil
	} else {
		logs.Error("GetOriginPoc, cve_origin_upstream_poc, "+
			"cve_id: ", cveId, ", Query failed")
	}
	return err
}

func GetOriginEvent(cveId int64, oue *OriginUpstreamEvent) (err error) {
	o := orm.NewOrm()
	err = o.Raw("select * FROM cve_origin_upstream_event "+
		"WHERE cve_id=?", cveId).QueryRow(oue)
	if err == nil {
		logs.Info("GetOriginEvent, cve_origin_upstream_event, cve_id: ", cveId, ", search successful")
		return nil
	} else {
		logs.Error("GetOriginEvent, cve_origin_upstream_event, cve_id: ", cveId, ", Query failed")
	}
	return err
}

func GetOriginReference(cveId int64, our *[]OriginUpstreamReference) (num int64, err error) {
	o := orm.NewOrm()
	num, err = o.Raw("select * FROM cve_origin_upstream_reference "+
		"WHERE cve_id=?", cveId).QueryRows(our)
	if num > 0 && err == nil {
		logs.Info("GetOriginReference, cve_origin_upstream_reference, "+
			"cve_id: ", cveId, ", search successful")
		return num, nil
	} else {
		logs.Error("GetOriginReference, cve_origin_upstream_reference, "+
			"cve_id: ", cveId, ", Query failed")
	}
	return 0, err
}

func GetOriginVulType(cveId int64, ouv *OriginUpstreamVulType) (err error) {
	o := orm.NewOrm()
	err = o.Raw("select * FROM cve_origin_upstream_vul_type "+
		"WHERE cve_id=?", cveId).QueryRow(ouv)
	if err == nil {
		logs.Info("GetOriginVulType, cve_origin_upstream_vul_type, "+
			"cve_id: ", cveId, ", search successful")
		return nil
	} else {
		logs.Error("GetOriginVulType, cve_origin_upstream_vul_type, "+
			"cve_id: ", cveId, ", Query failed")
	}
	return err
}

func GetOriginFixSuggest(cveId int64, ouf *OriginUpstreamFixSuggest) (err error) {
	o := orm.NewOrm()
	err = o.Raw("select * FROM cve_origin_upstream_fix_suggest "+
		"WHERE cve_id=?", cveId).QueryRow(ouf)
	if err == nil {
		logs.Info("GetOriginFixSuggest, cve_origin_upstream_fix_suggest, "+
			"cve_id: ", cveId, ", search successful")
		return nil
	} else {
		logs.Error("GetOriginFixSuggest, cve_origin_upstream_fix_suggest, "+
			"cve_id: ", cveId, ", Query failed")
	}
	return err
}

func GetOriginFixSuggestRef(fixId int64, oufr *[]OriginUpstreamFixSuggestRef) (num int64, err error) {
	o := orm.NewOrm()
	num, err = o.Raw("select * FROM cve_origin_upstream_fix_suggest_ref "+
		"WHERE fix_id=?", fixId).QueryRows(oufr)
	if num > 0 && err == nil {
		logs.Info("GetOriginFixSuggestRef, cve_origin_upstream_fix_suggest_ref, "+
			"fix_id: ", fixId, ", search successful")
		return num, nil
	} else {
		logs.Error("GetOriginFixSuggestRef, cve_origin_upstream_fix_suggest_ref, "+
			"fix_id: ", fixId, ", Query failed")
	}
	return 0, err
}

func GetOriginFixSuggestRefTag(fixRefId int64, oufrt *[]OriginUpstreamFixSuggestRefTag) (num int64, err error) {
	o := orm.NewOrm()
	num, err = o.Raw("select * FROM cve_origin_upstream_fix_suggest_ref_tag "+
		"WHERE fix_ref_id=?", fixRefId).QueryRows(oufrt)
	if num > 0 && err == nil {
		logs.Info("GetOriginFixSuggestRefTag, cve_origin_upstream_fix_suggest_ref_tag, "+
			"fix_ref_id: ", fixRefId, ", search successful")
		return num, nil
	} else {
		logs.Error("GetOriginFixSuggestRefTag, cve_origin_upstream_fix_suggest_ref_tag, "+
			"fix_ref_id: ", fixRefId, ", Query failed")
	}
	return 0, err
}
